package com.ba.base.view

import android.Manifest
import android.net.Uri
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.ba.base.R
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.tbruyelle.rxpermissions2.RxPermissions
import kotlinx.android.synthetic.main.item_file.view.*
import qsos.core.lib.utils.image.GlideApp
import qsos.lib.base.base.adapter.BaseAdapter
import qsos.lib.base.base.adapter.BaseNormalHolder
import qsos.lib.base.base.holder.BaseHolder
import qsos.lib.base.callback.OnTListener
import qsos.lib.base.utils.ToastUtils

/**
 *
 *
 * 添加文件列表
 */
class AddFileAdapter<T> : BaseAdapter<AddFileAdapter.TemFile<T>>(arrayListOf()) {

    class TemFile<T> {
        var add: Boolean = false
        var uri: Uri? = null
        var file: T? = null
        var video: Boolean = false

        constructor(add: Boolean) {
            this.add = add
        }

        constructor(uri: Uri, file: T, video: Boolean) {
            this.uri = uri
            this.file = file
            this.video = video
        }
    }

    private var mListener: OnTListener<TemFile<T>>? = null

    fun setListener(listener: OnTListener<TemFile<T>>) {
        this.mListener = listener
    }

    override fun getHolder(view: View, viewType: Int): BaseHolder<TemFile<T>> {
        return when (viewType) {
            R.layout.item_file_add -> BaseNormalHolder(view,
                setHolder = { holder, data, _ ->
                    holder.itemView.setOnClickListener {
                        RxPermissions(holder.itemView.context as AppCompatActivity).request(
                            Manifest.permission.CAMERA,
                            Manifest.permission.RECORD_AUDIO,
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE
                        ).subscribe {
                            if (it) {
                                this.mListener?.back(data)
                            } else {
                                ToastUtils.showToast(holder.itemView.context, "请前往手机设置开启文件权限")
                            }
                        }
                    }
                })
            else -> BaseNormalHolder(view,
                setHolder = { holder, data, _ ->
                    holder.itemView.item_file_video.visibility = if (data.video) {
                        View.VISIBLE
                    } else {
                        View.GONE
                    }
                    GlideApp.with(holder.itemView.context)
                        .load(data.uri)
                        .diskCacheStrategy(DiskCacheStrategy.DATA)
                        .skipMemoryCache(true)
                        .dontAnimate()
                        .into(holder.itemView.item_file_avatar)
                    holder.itemView.item_file_delete.setOnClickListener {
                        this.mListener?.back(data)
                    }
                })
        }
    }

    override fun getLayoutId(viewType: Int): Int {
        return viewType
    }

    override fun getItemViewType(position: Int): Int {
        return if (data[position].add) R.layout.item_file_add else R.layout.item_file
    }

    override fun onItemClick(view: View, position: Int, obj: Any?) {}

    override fun onItemLongClick(view: View, position: Int, obj: Any?) {}

}